export function fix2(number, num = 2) {
    if (number) {
        let newnum = Number(number)
        if (String(newnum).indexOf('.') === -1) {
            return newnum
        } else if (String(newnum).split('.')[1].length <= 2) {
            return newnum
        } else return Number(newnum.toFixed(num));

    } else {
        return 0
    }

}

export function timeStampToString(str, format = "Y-m-d H:i:s") {
    format = format.toLowerCase();

    let formatArr = ["y", "m", "d", "h", "i", "s"];

    let timeData = format;

    let dateNum = new Date(str * 1000);

    formatArr.forEach((item) => {

        if (format.search(item) !== -1) {

            switch (item) {

                case "y": {
                    timeData = timeData.replace(eval("/" + item + "/g"), dateNum.getFullYear())
                    break;
                }

                case "m": {
                    let MM = dateNum.getMonth() + 1;
                    MM = MM < 10 ? ('0' + MM) : MM; //月补0
                    timeData = timeData.replace(eval("/" + item + "/g"), MM)
                    break;
                }

                case "d": {
                    let d = dateNum.getDate();
                    d = d < 10 ? ('0' + d) : d; //天补0
                    timeData = timeData.replace(eval("/" + item + "/g"), d)
                    break;
                }

                case "h": {
                    let h = dateNum.getHours();
                    h = h < 10 ? ('0' + h) : h; //小时补0
                    timeData = timeData.replace(eval("/" + item + "/g"), h)
                    break;
                }

                case "i": {
                    let m = dateNum.getMinutes();
                    m = m < 10 ? ('0' + m) : m; //分钟补0
                    timeData = timeData.replace(eval("/" + item + "/g"), m)
                    break;
                }

                case "s": {
                    let s = dateNum.getSeconds();
                    s = s < 10 ? ('0' + s) : s; //秒补0

                    timeData = timeData.replace(eval("/" + item + "/g"), s)
                    break;
                }

                default:
            }

        }
    })

    return timeData;
}

export function getrechargetype(num) {
    switch (parseInt(num)) {
        case 1:
            return '银行卡';
            break;
        case 2:
            return '支付宝';
            break;
        case 3:
            return 'USDT-TRC20';
            break;

        case 4:
            return 'USDT-ERC20';
            break;
    }
}

export function cycletype(num) {
    if (num === undefined || num == null) {
        return;
    }
    let typelist = [{
        label: '分钟',
        value: 0
    },
        {
            label: '小时',
            value: 1
        },
        {
            label: '天',
            value: 2
        },
        {
            label: '周',
            value: 3
        },
        {
            label: '月',
            value: 4
        }
    ]
    return typelist.find(item => item.value == Number(num)).label;
}

export function getrechargecheck(num) {
    switch (parseInt(num)) {
        case 0:
            return '待验证';
            break;
        case 1:
            return '成功';
            break;
        case 2:
            return '失败';
            break;
    }
}

export function getwithexamine(num) {
    switch (parseInt(num)) {
        case 0:
            return '待验证';
            break;
        case 1:
            return '成功';
            break;
        case 2:
            return '拒绝';
            break;
    }
}

export function wait( time = 1000) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, time)
    })
}

export function float_format(value) {
    if (value) {
        let newstr = value.toString();
        // 使用正则表达式去掉小数点后面多余的零，但保留小数点和至少一个非零数字
        if (newstr.includes('.')) {
            newstr = newstr.replace(/(\.\d*[1-9])0+$/, "$1").replace(/\.0+$/, "");
        }
        return newstr;
    } else {
        return 0;
    }
}

export function money_format(val) {
    const new_val = float_format(val)
    return new_val.length > 6 ? new_val.slice(0, 6) + '...' : new_val;
    ;
}

